package base;

import java.util.*;

/**
 * the class which saves a certain order's all orderItems
 */

public class Order {
    //to save all orderItems.
    private Vector<OrderItem> orderItems = new Vector<OrderItem>();
    /**
     * to add the orderItem to the list.
     */
    public void addOrderItem(OrderItem orderItem) {
        orderItems.add(orderItem);
    }

    /**
     * to delete the orderItem which user wants to delete.
     */
    public void removeOrderItem(OrderItem orderItem) {
        orderItems.remove(orderItem);
    }

    /**
     * Returns an iterator over the items in this orderItems.
     *
     * return  an {@link Iterator}
     */
    public Iterator<OrderItem> getItemsIterator() {

        return orderItems.iterator();
    }

    /**
     * to search the orderItem which user wants to find in the list.
     * return the orderItem which user wants to find
     */
    public OrderItem getOrderItem(Product product) {
        for (OrderItem orderItem : orderItems) {
            if (orderItem.getProduct().equals(product)) {
                return orderItem;
            }
        }
        return null;
    }

    /**
     * return the number of orderItems which are saved in the list.
     */
    public int getNumberOfOrderItems() {
        return this.orderItems.size();
    }

    /**
     * to sum all the orderItems' value in the list.
     * return the sum of all the orderItems' value.
     */
    public double getValue() {
        double value = 0.0;
        for (OrderItem orderItem : orderItems) {
            value += orderItem.getValue();
        }
        return value;
    }
    //in Order
    @Override
    public String toString() {
        String ret = "";
        ret += "-----------ORDER-----------\n";
        ret += "Quantity|Code|Unit  |Total \n";
        Iterator<OrderItem> it = orderItems.iterator();
        while(it.hasNext()) {
            ret += it.next();
        }
        ret += "Total Price:" + String.format("%.2f", getValue()) + '\n';
        ret += "------------END------------\n";
        return ret;
    }
}


